<?php
ob_start();

define('ROOT_PATH', dirname(__FILE__));
$_SESSION['path']=ROOT_PATH;
require_once ROOT_PATH . '/lib/common/CommonFunctions.php';
require_once ROOT_PATH . '/lib/models/eimadmin/EmployStat.php';

require_once ROOT_PATH . '/lib/common/Language.php';
$lan = new Language();
//require_once ROOT_PATH . '/language/default/lang_default_full.php';
//require_once ROOT_PATH . '/lib/common/xajax/xajax.inc.php';
//require_once ROOT_PATH . '/lib/common/xajax/xajaxElementFiller.php';
require_once($lan->getLangPath("full.php"));

session_start();

// To test a different style, can use http://host/orangehrm/login.php?styleSheet=abc
$styleSheet = CommonFunctions::getTheme();
$_SESSION['styleSheet'] = $styleSheet;

$wpath = explode('/login.php', $_SERVER['REQUEST_URI']);
$_SESSION['WPATH']= $wpath[0];

require_once ROOT_PATH . '/lib/models/eimadmin/Login.php';
require_once ROOT_PATH . '/lib/common/authorize.php';

// LDAP Module 

$ldapFile = ROOT_PATH . "/plugins/ldap/LdapLogin.php";
$_SESSION['ldap'] = "enabled";
$_SESSION['ldapStatus'] = "enabled";

if (file_exists($ldapFile)) {
	require_once ROOT_PATH . '/plugins/PlugInFactoryException.php';
	require_once ROOT_PATH . '/plugins/PlugInFactory.php';
	$_SESSION['ldap'] = "enabled";
	require_once $ldapFile;
	$ldap = PlugInFactory::factory("LDAP");
	
	if($ldap->checkAuthorizeLoginUser("Admin") && $ldap->checkAuthorizeModule("Admin")){
		$ldapStatus = $ldap->retrieveLdapStatus();
		$_SESSION['ldapStatus'] = $ldapStatus;
	}else{
		throw new PlugInFactoryException(PlugInFactoryException::PLUGIN_INSTALL_ERROR);
	}
}

// LDAP Module 

// Print Benefits Module 

$benefitsFile = ROOT_PATH . "/plugins/printBenefits/pdfHspSummary.php";
$_SESSION['printBenefits'] = "disabled";

if (file_exists($benefitsFile)) {
	$_SESSION['printBenefits'] = "enabled";
}


// Print Benefits Module 
//$txtPassword = base64_decode($_POST['txtPassword']);

if ((isset($_GET['actionID'])) && base64_decode($_GET['actionID']) == 'chkAuthentication')  
{			
	$login = new Login();	
	
	$username = base64_decode(trim($_GET['txtUserName']));	
	
	$rset=$login->filterUser($username);
	//$rset=$login->filterUser(trim($_GET['txtUserName']));
	
	//error_log(md5(""),3,"C:/logtest.log");
	if ($_SESSION['ldapStatus'] == "enabled") {
		
		
			//$ldapAuth = $ldap->ldapAuth($rset[0][0], $txtPassword);
						
			$ldapAuth = $ldap->ldapAuthWithoutPass($rset[0][0]);
			
			if ($ldapAuth) { // stuff in normal login process
					
				
				$_SESSION['ladpUser'] = true;				

				if ($rset[0][5]=='Enabled') {

					
					if (($rset[0][7] == "Yes") || (($rset[0][7] == "No") && !empty($rset[0][6]))) {
						#die($username);
						$_SESSION['user']=$rset[0][3];
						$_SESSION['userGroup']=$rset[0][4];
						$_SESSION['isAdmin']=$rset[0][7];
						$_SESSION['empID']=$rset[0][6];

						$_SESSION['fname']=$rset[0][2];

						// If not an admin user, check if a supervisor and/or project admin 
						$isSupervisor = false;
						$isProjectAdmin = false;
                		$isManager = false;
                		$isDirector = false;
						if ($_SESSION['isAdmin'] == 'No') {

						$authorizeObj = new authorize($_SESSION['empID'], $_SESSION['isAdmin']);
						$isSupervisor = $authorizeObj->isSupervisor();
						$isProjectAdmin = $authorizeObj->isProjectAdmin();
                    	$isManager = $authorizeObj->isManager();
                    $isDirector = $authorizeObj->isDirector();
					}
					$_SESSION['isSupervisor'] = $isSupervisor;
					$_SESSION['isProjectAdmin'] = $isProjectAdmin;
                	$_SESSION['isManager'] = $isManager;
					$_SESSION['isDirector'] = $isDirector;

					$wpath = explode('/login.php', $_SERVER['REQUEST_URI']);
					$_SESSION['WPATH']= $wpath[0];

					// TODO: Can set user specific stylesheet here.
					$_SESSION['styleSheet'] = $styleSheet;

					setcookie('Loggedin', 'True', 0, '/');
					
					$url_back = base64_decode($_GET['url_redirect']);
					#die('test');					
					//header("Location: ./index.php");
					header("Location: ./autoindex.php?url_redirect=" . $url_back);					
					} else {
						$InvalidLogin=3;
					}
				} else {
					$InvalidLogin=2;
				}
			} else {
				$InvalidLogin = 1;
			}

	}else if (base64_decode($_GET['txtUserName']) == $rset[0][0]) {//if (md5($txtPassword) == $rset[0][1]) {
		if ($rset[0][8] == EmploymentStatus::EMPLOYMENT_STATUS_ID_TERMINATED) {
			$InvalidLogin=5;
		} else if ($rset[0][5]=='Enabled') {
			if (($rset[0][7] == "Yes") || (($rset[0][7] == "No") && !empty($rset[0][6]))) {
				$_SESSION['user']=$rset[0][3];
				$_SESSION['userGroup']=$rset[0][4];
				$_SESSION['isAdmin']=$rset[0][7];
				$_SESSION['empID']=$rset[0][6];

				$_SESSION['fname']=$rset[0][2];

				// If not an admin user, check if a supervisor and/or project admin 
				$isSupervisor = false;
				$isProjectAdmin = false;
           		$isManager = false;
                $isDirector = false;
                $isAcceptor = false;
                $isOfferer = false;

				if ($_SESSION['isAdmin'] == 'No') {

					$authorizeObj = new authorize($_SESSION['empID'], $_SESSION['isAdmin']);
					$isSupervisor = $authorizeObj->isSupervisor();
					$isProjectAdmin = $authorizeObj->isProjectAdmin();
                   	$isManager = $authorizeObj->isManager();
                    $isDirector = $authorizeObj->isDirector();
                    $isAcceptor = $authorizeObj->isAcceptor();
                    $isOfferer = $authorizeObj->isOfferer();

				}
				$_SESSION['isSupervisor'] = $isSupervisor;
				$_SESSION['isProjectAdmin'] = $isProjectAdmin;
                $_SESSION['isManager'] = $isManager;
				$_SESSION['isDirector'] = $isDirector;
				$_SESSION['isAcceptor'] = $isAcceptor;
				$_SESSION['isOfferer'] = $isOfferer;

				$wpath = explode('/login.php', $_SERVER['REQUEST_URI']);
				$_SESSION['WPATH']= $wpath[0];

				// TODO: Can set user specific stylesheet here.
				$_SESSION['styleSheet'] = $styleSheet;

				setcookie('Loggedin', 'True', 0, '/');

				header("Location: ./index.php");
			} else {
				$InvalidLogin=3;
			}
		} else $InvalidLogin=2;
	} else {
		$InvalidLogin=1;
	}
}
require_once ROOT_PATH . '/lib/extractor/news/EXTRACTOR_newslist.php';
require_once ROOT_PATH . '/lib/extractor/news/EXTRACTOR_news.php';
require_once ROOT_PATH . '/lib/controllers/NewsController.php';

$viewListExtractor = new EXTRACTOR_newsList();	
$newsObj = new NewsController();
$searchObject = $viewListExtractor->parseSearchData($_POST, $_GET);
$objResult= $newsObj->_viewListNewsPublic($searchObject);	

$listNews = $objResult[0];
$listEvent = $objResult[1];
$listAnnounce = $objResult[2];
$baseURL = './lib/controllers/CentralController.php?newscode=Vacancy';